package com.ynding.spring.idea.ddd.core.model.lottery.domain;

import com.ynding.spring.idea.ddd.common.service.integration.lottery.dto.MtCityInfoDTO;
import com.ynding.spring.idea.ddd.core.model.bo.AggregateRoot;
import com.ynding.spring.idea.ddd.core.model.lottery.dp.LotteryID;
import com.ynding.spring.idea.ddd.core.model.lottery.valobj.AwardPool;
import com.ynding.spring.idea.ddd.core.model.lottery.valobj.DrawLotteryContext;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @author ynding
 * @version 2022/9/3
 **/
@Data
@ApiModel(value = "DrawLottery", description = "抽奖")
public class DrawLottery implements AggregateRoot<LotteryID> {

    @ApiModelProperty("抽奖id")
    private LotteryID id;

    @ApiModelProperty("奖池")
    private List<AwardPool> awardPools;

    public void setLotteryId(LotteryID lotteryId) {
        if (lotteryId == null) {
            throw new IllegalArgumentException("非法的抽奖id");
        }
        this.id = lotteryId;
    }

    /**
     * 根据场景选择奖池
     *
     * @param context 抽奖场景
     * @return 奖池
     */
    public AwardPool chooseAwardPool(DrawLotteryContext context) {
        if (context.getMtCityInfo() != null) {
            return chooseAwardPoolByCityInfo(awardPools, context.getMtCityInfo());
        } else {
            return chooseAwardPoolByScore(awardPools, context.getGameScore());
        }
    }

    /**
     * 根据抽奖所在城市选择奖池
     *
     * @param awardPools
     * @param cityInfo
     * @return
     */
    private AwardPool chooseAwardPoolByCityInfo(List<AwardPool> awardPools, MtCityInfoDTO cityInfo) {
        for (AwardPool awardPool : awardPools) {
            if (awardPool.matchedCity(cityInfo.getCityId())) {
                return awardPool;
            }
        }
        return null;
    }

    /**
     * 根据抽奖活动得分选择奖池
     *
     * @param awardPools
     * @param gameScore
     * @return
     */
    private AwardPool chooseAwardPoolByScore(List<AwardPool> awardPools, int gameScore) {
        // TODO
        return null;
    }
}
